package com.zengguang.testkaoshi.services.impl;

import com.zengguang.testkaoshi.dao.StudentInfoDao;
import com.zengguang.testkaoshi.dao.TeacherInfoDao;
import com.zengguang.testkaoshi.pojo.TeacherInfo;
import com.zengguang.testkaoshi.services.ITeacherInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherInfoService implements ITeacherInfoService {
    @Autowired
    private TeacherInfoDao teacherInfoDao;
    //老师信息插入
    @Override
    public int insertTeacherInfo(TeacherInfo teacherInfo) {
            int returnInt=0;
            /*
             * 1.业务判断
             * (1)老师id不能重复
             */
            int teacherIDCount=teacherInfoDao.getTeacherIDCount(teacherInfo.getTeacherinfoid());
            if (teacherIDCount<1){
                //2、数据添加
                int addClassInt=teacherInfoDao.insertTeacherInfo(teacherInfo);
                //3、返回值设置
                returnInt=addClassInt;
            }else {
                returnInt=-1;//表示有相同id,添加失败
            }

            return returnInt;
    }
    //老师信息修改
    @Override
    public int updateTeacher(TeacherInfo teacherInfo) {
            int returnInt=0;
            /*
             * 1.业务判断
             * (1)根据老师信息查询
             * （2）老师修改
             */
            int teacherIDCount=teacherInfoDao.getTeacherIDCount(teacherInfo.getTeacherinfoid());
            if (teacherIDCount>0){
                //2、数据修改
                int updateTeacherInt=teacherInfoDao.updateTeacher(teacherInfo);
                //3、返回值设置
                returnInt=updateTeacherInt;
            }else {
                returnInt=-1;//表示有相同id,添加失败
            }
            return returnInt;
    }
    //查询全部
    @Override
    public List<TeacherInfo> selectAllTeacher() {
            List<TeacherInfo> list=teacherInfoDao.selectAllTeacher();
            return list;
    }
    //根据老师id删除
    @Override
    public int deleteTeacher(int teacherinfoid) {
            int temp=0;
            try{
                temp=teacherInfoDao.getTeacherIDCount(teacherinfoid);
                if (temp>0){
                    temp=teacherInfoDao.deleteTeacher(teacherinfoid);
                }else{
                    return -1;
                }
            }catch (Exception e){
                return -1;
            }
            return temp;
        }

}
